$(function () {
    /**
     系统设置
     **/
    function doSysSettings(processName, processId, parameterName, parameterId, componentName, componentId) {
        $.ajax({
            // url: '../json/getTask.json',
            url: baseUrl + 'doSysSettings',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {
                processName: processName,
                processId: processId,
                parameterName: parameterName,
                parameterId: parameterId,
                componentName: componentName,
                componentId: componentId
            },
        })
            .done(function (data) {
                console.log("doSysSettings");
                if (data.code == 0) {
                    layer.open({
                        title: '信息',
                        content: '配置成功！'
                    });
                } else if (data.code == 1) {
                    layer.open({
                        title: '信息',
                        content: '插入失败！'
                    });
                } else if (data.code == 2) {
                    layer.open({
                        title: '信息',
                        content: '参数或部件重复！'
                    });
                } else if (data.code == 3) {
                    layer.open({
                        title: '信息',
                        content: '已有工序包含该参数！'
                    });
                } else if (data.code == 4) {
                    layer.open({
                        title: '信息',
                        content: '已有工序包含该部件！'
                    });
                } else if (data.code == 5) {
                    layer.open({
                        title: '信息',
                        content: '已有工序包含所选部件或参数！'
                    });
                }
            })
            .fail(function () {
                console.log("error");
            })
    }

    // doSysSettings();


    $("body").on('click', '.doSysSettings', function (event) {
        event.preventDefault();
        /* Act on the event */
        var processName = $(".processName").val().trim();
        var processId = $(".processId").val().trim();
        var parameterName = $(".parameterName").val();
        var parameterId = $(".parameterId").val();
        var componentName = $(".componentName").val();
        var componentId = $(".componentId").val();


        if (processName == "" || processId == "") {
            layer.open({
                title: '信息',
                content: '请填写工序名称和工序号！'
            });
        } else {
            layer.confirm('是否进行添加？', function (index) {
                //do something
                if (parameterId == "" && componentId == "") {
                    doSysSettings(processName, Number(processId), parameterName, parameterId, componentName, componentId);
                } else if (parameterId == "") {
                    doSysSettings(processName, Number(processId), parameterName, parameterId, componentName, Number(componentId));
                } else if (componentId == "") {
                    doSysSettings(processName, Number(processId), parameterName, Number(parameterId), componentName, componentId);
                }

                // layer.close(index);
                window.location.reload();
            });
        }


    });

    /**
     查找系统设置
     **/
    function getSysSettings() {

        $.ajax({
            // url: '../json/getSysSettings.json',
            url: baseUrl + 'getSysSettings',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            // data: { method: 'getSysSettings' },
        })
            .done(function (data) {
                console.log("getSysSettings");
                var data = data.data;
                var html = "";

                for (var i = 0; i < data.length; i++) {

                    var rowspan = data[i].componChildren.length;

                    if (data[i].componChildren.length < data[i].paramChildren.length) {
                        rowspan = data[i].paramChildren.length;
                    }
                    if ((data[i].paramChildren.length != 0 && data[i].componChildren.length != 0) && (data[i].paramChildren.length == data[i].componChildren.length)) {
                        html += `<tr><td rowspan="${rowspan}">${data[i].processName}</td>
                            <td rowspan="${rowspan}">${data[i].processId}</td>
                            <td>${data[i].paramChildren[0].parameterName}</td>
                            <td>${data[i].paramChildren[0].parameterId}</td>
                            <td>${data[i].componChildren[0].componentName}</td>
                            <td>${data[i].componChildren[0].componentId}</td>
                            </tr>`;
                        for (var j = 1; j < data[i].paramChildren.length; j++) {
                            html += `<tr>
                             <td>${data[i].paramChildren[j].parameterName}</td>
                            <td>${data[i].paramChildren[j].parameterId}</td>
                             <td>${data[i].componChildren[j].componentName}</td>
                            <td>${data[i].componChildren[j].componentId}</td></tr>
                            `;
                        }
                    } else if ((data[i].paramChildren.length != 0 && data[i].componChildren.length != 0) && (data[i].paramChildren.length > data[i].componChildren.length)) {
                        html += `<tr><td rowspan="${rowspan}">${data[i].processName}</td>
                            <td rowspan="${rowspan}">${data[i].processId}</td>
                            <td>${data[i].paramChildren[0].parameterName}</td>
                            <td>${data[i].paramChildren[0].parameterId}</td>
                            <td>${data[i].componChildren[0].componentName}</td>
                            <td>${data[i].componChildren[0].componentId}</td>
                            </tr>`;
                        var com = data[i].componChildren.length - 1;


                        for (var j = 1; j < data[i].paramChildren.length; j++) {
                            if (com >= j) {
                                html += `<tr>
                             <td>${data[i].paramChildren[j].parameterName}</td>
                            <td>${data[i].paramChildren[j].parameterId}</td>
                             <td>${data[i].componChildren[j].componentName}</td>
                            <td>${data[i].componChildren[j].componentId}</td></tr>
                            `;
                            } else {
                                html += `<tr>
                             <td>${data[i].paramChildren[j].parameterName}</td>
                            <td>${data[i].paramChildren[j].parameterId}</td>
                             <td></td>
                            <td></td></tr>
                            `;
                            }
                        }
                    } else if ((data[i].paramChildren.length != 0 && data[i].componChildren.length != 0) && (data[i].paramChildren.length < data[i].componChildren.length)) {
                        html += `<tr><td rowspan="${rowspan}">${data[i].processName}</td>
                            <td rowspan="${rowspan}">${data[i].processId}</td>
                            <td>${data[i].paramChildren[0].parameterName}</td>
                            <td>${data[i].paramChildren[0].parameterId}</td>
                            <td>${data[i].componChildren[0].componentName}</td>
                            <td>${data[i].componChildren[0].componentId}</td>
                            </tr>`;
                        var par = data[i].paramChildren.length - 1;
                        for (var j = 1; j < data[i].componChildren.length; j++) {
                            if (par >= j) {
                                html += `<tr>
                             <td>${data[i].paramChildren[j].parameterName}</td>
                            <td>${data[i].paramChildren[j].parameterId}</td>
                             <td>${data[i].componChildren[j].componentName}</td>
                            <td>${data[i].componChildren[j].componentId}</td></tr>
                            `;
                            } else {
                                html += `<tr>
                             <td></td>
                            <td></td>
                             <td>${data[i].componChildren[j].componentName}</td>
                            <td>${data[i].componChildren[j].componentId}</td></tr>
                            `;
                            }

                        }

                    } else if (data[i].paramChildren.length == 0 && data[i].componChildren.length != 0) {
                        html += `<tr><td rowspan="${rowspan}">${data[i].processName}</td>
                            <td rowspan="${rowspan}">${data[i].processId}</td>
                            <td></td>
                            <td></td>
                            <td>${data[i].componChildren[0].componentName}</td>
                            <td>${data[i].componChildren[0].componentId}</td>
                            </tr>`;
                        for (var j = 1; j < data[i].componChildren.length; j++) {
                            html += `<tr>
                             <td></td>
                            <td></td>
                             <td>${data[i].componChildren[j].componentName}</td>
                            <td>${data[i].componChildren[j].componentId}</td></tr>
                            `;
                        }
                    } else if (data[i].componChildren.length == 0 && data[i].paramChildren.length != 0) {
                        html += `<tr><td rowspan="${rowspan}">${data[i].processName}</td>
                            <td rowspan="${rowspan}">${data[i].processId}</td>
                            <td>${data[i].paramChildren[0].parameterName}</td>
                            <td>${data[i].paramChildren[0].parameterId}</td>
                            <td></td>
                            <td></td>
                            </tr>`;
                        for (var j = 1; j < data[i].paramChildren.length; j++) {
                            html += `<tr>
                             <td>${data[i].paramChildren[j].parameterName}</td>
                            <td>${data[i].paramChildren[j].parameterId}</td>
                             <td></td>
                            <td></td></tr>
                            `;
                        }
                    } else if (data[i].componChildren.length == 0 && data[i].paramChildren.length == 0) {
                        html += `<tr><td rowspan="1">${data[i].processName}</td>
                            <td rowspan="1">${data[i].processId}</td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            </tr>`;
                    }


                }


                // }

                $(html).appendTo('.process');
            })
            .fail(function () {
                console.log("error");
            })
    }

    getSysSettings();

})